<template>
  <div class="app-container">
    <u-page title="生成工牌">
      <u-filtered>
        <el-form :inline="true" :model="listQuery" class="search-form">
          <el-form-item label="入职时间">
            <u-date-time-picker v-model="searchDate" @change="changeSearchDate" />
          </el-form-item>
          <el-form-item label="部门" prop="dept">
            <el-input v-model="listQuery.dept" placeholder="关键词搜索" clearable />
          </el-form-item>
          <el-form-item label="商户" prop="merchantName">
            <el-input v-model="listQuery.merchantName" placeholder="关键词" clearable />
          </el-form-item>
          <el-form-item label="楼层" prop="floor">
            <mall-floor v-model="listQuery.floor" />
          </el-form-item>
          <el-form-item label="姓名" prop="name">
            <el-input v-model="listQuery.name" placeholder="关键词" clearable />
          </el-form-item>
          <el-form-item label="工号" prop="jobNo">
            <el-input v-model="listQuery.jobNo" placeholder="关键词" clearable />
          </el-form-item>
          <el-form-item label="手机号" prop="mobile">
            <el-input v-model="listQuery.mobile" placeholder="关键词" clearable />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" @click="hdlFilter">搜索</el-button>
          </el-form-item>
        </el-form>
      </u-filtered>

      <el-descriptions :column="2" border>
        <el-descriptions-item>
          <u-table
            :columns="columns"
            :data="list"
            :fetch="hdlList"
            :loading="listLoading"
            :options="listOptions"
            :pagination.sync="listQuery"
            @selection-change="hdlSelectChange"
          >
            <template v-slot:right>
              <el-table-column label="操作" align="center" fixed="right">
                <template slot-scope="{ row }">
                  <el-button type="text" @click="hdlCreate(row)">生成工牌</el-button>
                </template>
              </el-table-column>
            </template>
          </u-table>
        </el-descriptions-item>
        <el-descriptions-item>
          <div v-if="data.badgeVisible" ref="capture" class="badge">
            <span class="company">{{ data.company }}</span>
            <div class="avatar-placeholder">
              <img class="avatar" :src="data.avatarUrl" :fit="data.avatarfit" alt="头像">
            </div>
            <span class="name">{{ data.name }}</span>
            <img class="corner-image" :src="data.cornerImage" :fit="data.fit" alt="角落图片">
          </div>
          <el-button type="primary" @click="saveAsImage()">保存为图片</el-button>
        </el-descriptions-item>
      </el-descriptions>
    </u-page>
  </div>
</template>

<script>
import html2canvas from 'html2canvas'
import MallFloor from '@/components/mall/floor.vue'
export default {
  components: { MallFloor },
  data() {
    return {
      api: this.$u.api.PluginsGuide,
      // 表格
      columns: [
        {
          prop: 'jobNo',
          label: '工号'
        },
        {
          prop: 'name',
          label: '姓名'
        },
        {
          prop: 'mobile',
          label: '手机号'
        },
        {
          prop: 'dept',
          label: '部门'
        },
        {
          prop: 'enterpriseName',
          label: '公司'
        },
        {
          prop: 'merchantName',
          label: '商户'
        },
        {
          prop: 'floor',
          label: '楼层'
        },
        {
          prop: 'createTime',
          label: '入职时间'
        },
        {
          prop: 'status',
          label: '状态',
          render: (h, params) => {
            const { status } = params.row
            if (status === 0) {
              return h('el-tag', { props: { type: 'warning' }}, '待入职')
            } else if (status === 1) {
              return h('el-tag', { props: { type: 'success' }}, '正常')
            } else if (status === 2) {
              return h('el-tag', { props: { type: 'info' }}, '离职')
            } else if (status === 3) {
              return h('el-tag', { props: { type: 'danger' }}, '锁定')
            }
          }
        }
      ],
      list: [],
      listLoading: false,
      listOptions: {
        mutiSelect: true, // boolean 是否多选
        stripe: true // boolean 斑马纹
      },
      listQuery: {
        pageNumber: 1,
        pageSize: 10,
        totalCount: 1,
        beginTime: '',
        endTime: '',
        name: '',
        mobile: '',
        status: '1'
      },
      // 表格
      data: {
        avatarfit: '',
        cornerImagefit: '',
        name: '',
        company: '',
        cornerImage: 'https://csms.chiconysquare.net/assets/logo/brandlogo.png',
        badgeVisible: true,
        avatarUrl: ''
      },
      imagesLoaded: false,
      dialogStatus: '',
      dataForm: {}

    }
  },
  created() {
    this.hdlList()
  },
  methods: {
    hdlCreate(row) {
      console.log(row)
      // this.data = row
      this.data.avatarfit = 'cover'
      this.data.cornerImagefit = ''
      this.data.name = row.name
      this.data.company = row.merchantName
      this.data.cornerImage = 'https://csms.chiconysquare.net/assets/logo/brandlogo.png'
      this.data.avatarUrl = 'https://csms.chiconysquare.net/' + row.avatar
      this.data.badgeVisible = true
    },
    saveAsImage() {
      const element = this.$refs.capture
      html2canvas(element).then((canvas) => {
        const link = document.createElement('a')
        link.href = canvas.toDataURL('image/png')
        link.download = 'badge-image.png'
        link.click()
      })
    }

  }
}
</script>

<style scoped>
body {
  font-family: Arial, sans-serif;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 20px;
}
.badge {
  border: 1px solid #000;
  border-radius: 5px;
  width: 371px;
  height: 602px;
  padding: 20px;
  text-align: center;
  margin-top: 20px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  position: relative;
}
.company {
  //font-size: 65px; /* 放大一倍 */
  //font-size: 45px;
  font-weight: bold;
  //margin-bottom: 10px; /* 与头像的距离 */
  margin-top: 50px; /* 下移50px */

  display: -webkit-box; /* 使其成为弹性盒子 */
  -webkit-box-orient: vertical; /* 设置主轴方向为纵向 */
  -webkit-line-clamp: 2; /* 限制显示的行数为 2 */
  overflow: hidden; /* 隐藏多余内容 */
  transition: font-size 0.3s ease; /* 字体大小变化的过渡效果 */
  font-size: 50px; /* 默认字体大小 */
}

/* 当文本超过两行时缩小字体 */
@supports (-webkit-line-clamp: 2) {
  .company {
    font-size: 30px; /* 超过两行时缩小字体 */
  }
}

.name {
  font-size: 65px; /* 放大一倍 */
  font-weight: bold;
}
.avatar-placeholder {
  width: 245px; /* 椭圆形宽度 */
  height: 280px; /* 椭圆形高度 */
  border: 2px dashed #000;
  border-radius: 50%; /* 椭圆形 */
  margin: 20px auto;
  position: relative;
  overflow: hidden;
}
.avatar {
  width: 100%;
  height: 100%;
  object-fit: cover;
  position: absolute;
  top: 0;
  left: 0;
}
.corner-image {
  position: absolute;
  width: 147px;
  height: 49px;
  top: 20px;
  right: 20px;
  object-fit: cover; /* 确保图像适应框 */
}
</style>
